package cn.limbo.myandserver.utils;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;

/**
 * 获取安卓7.0系统的SD卡读写权限的工具类（9.0系统失效）
 */
public class PermissionUtils {

    // SD卡的读写权限
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    /**
     * 检查当前应用是否有SD卡的读写权限
     *
     * @param activity 当前的activity
     */
    public static void verifyStoragePermissions(Activity activity) {
        // 检查是否有“写”的权限
        int permission = ActivityCompat.checkSelfPermission(activity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // 没有权限，因此拒绝访问SD卡
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);
        }
    }
}
